/**
 * Created by qqli on 5/27/15.
 */
'use strict';

Ctrl.controller('RecycleBinCtrl', ['$scope', '$http', '$rootScope',
    function ($scope, $http, $rootScope) {
        $rootScope.title = 'Recycle Bin';
        $rootScope.header = 'Search';
        $rootScope.path = "recycle_bin";

        $rootScope.activeLi();
        $('footer a').html('Go to Dashboard chart').attr('href', HOST + 'admin/#/dashboard');

        // bind event
        $(window).resize(function () {
            resizeTable(100);
        });
        resizeTable(100);

        $scope.fixParams = {
            pageNo: 1
        };

        $scope.queryParams = {
            appName: $scope.appName,
            appCuReference: $scope.appCuReference,
            sourceChannel: $scope.sourceChannel,
            releaseStatus: $scope.releaseStatus,
            label: $scope.label,
            trash: 1
        };

        $scope.orderProp = 'appSequence';

        // query list
        var jqPagination = $('#pagination');
        $scope.queryApp = function (pageNo) {
            if (pageNo) {
                $scope.fixParams.pageNo = pageNo;
            }

            $http.post(HOST + 'app/res/list/' + $scope.fixParams.pageNo, $scope.queryParams)
                .success(function (data) {
                    if (data.status === 1) {
                        var _data = data.data;
                        $scope.apps = _data.appList;
                        $(".chk-all").removeClass('checked');
                        /**
                         * total page
                         * pagination(): make pagination
                         * jumpToPage(): input and turn to page
                         * @type {number}
                         */
                        var totalPage = Math.ceil(_data.total / _data.pageSize);
                        if (totalPage <= 1) {
                            jqPagination.html('');
                            return false;
                        }
                        pagination(jqPagination, _data.pageNo, totalPage, _data.total);
                        jumpToPage(jqPagination, totalPage, function (i) {
                            $scope.fixParams.pageNo = i;
                            $scope.queryApp();
                        });

                    } else if (data.status === -1) {
                        lozLogin();
                    }
                });
        };

        // click page num and turn to page
        jqPagination.on('click', '.goto', function () {
            $scope.fixParams.pageNo = $(this).data('page');
            $scope.queryApp();
            return false;
        });

        $scope.doRecovery = function () {
            var ids = getTableCheckedId();
            if (!ids.length) {
                alertModal('Please choose app to recover');
                return;
            }
            $http.post(HOST + 'app/recycle/recovery', ids)
                .success(function (data) {
                    if (data.status > 0) {
                        alertModal("Recovery Success");
                        $scope.queryApp();

                    } else if (data.status === -1) {
                        lozLogin();
                    }
                });
        };

        $scope.doDelete = function () {
            var ids = getTableCheckedId();
            if (!ids.length) {
                alertModal('Please choose app to delete');
                return;
            }

            function deleteRequest() {
                $("#confirmModal").modal("hide");

                $http.post(HOST + 'app/recycle/remove', ids)
                    .success(function (data) {
                        if (data.status > 0) {
                            $scope.queryApp();

                        } else if (data.status === -1) {
                            lozLogin();
                        }
                    });
            }

            var text = 'Do you really want to delete choosen apps thoroughly?' + '\u000d(this operation cannot be undone)';
            confirmModal(text, deleteRequest);
        };

        // init
        $scope.queryApp();
    }]);